from django.conf import settings
from django.core.cache import cache

from celery_task.celery import app
from home import models, serializer


@app.task()
def update_banner():
    queryset = models.Banner.objects.all().filter(is_delete=False).order_by('orders')[
               :settings.BANNER_COUNT]
    ser = serializer.BannerSerializer(instance=queryset, many=True)
    banner_list = ser.data
    for banner in banner_list:
        banner['image'] = settings.BACKEND_URL % str(banner['image'])
    cache.set('banner_list_cache', banner_list)
    return True


@app.task()
def update_num():
    res = models.Swimnum.objects.first()
    models.Swimnum.objects.update(num=0, total=0)
    models.SwimPeopleNum.objects.create(num=res.total)
    return True


@app.task()
def update_open():
    models.SwimmingsInfo.objects.update(state=True)
    return True


@app.task()
def update_close():
    models.SwimmingsInfo.objects.update(state=False)
    return True
